#!/bin/bash
# deploy k8s kata env
. /opt/hygon/common.sh

os_release=$(eval get_os_release)
case $os_release in
     "anolis" | "centos")
              #add kubernetes apt repository
              cp -f kubernetes_anolis.repo /etc/yum.repos.d/kubernetes.repo
              yum install -y kubectl kubelet kubeadm
              systemctl enable kubelet

              #config kubelete use contained runtime
              cp -f 0-containerd_anolis.conf  /usr/lib/systemd/system/kubelet.service.d/0-containerd.conf
              ;;

     "ubuntu")
              #download gpg key
              curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
              #add kubernetes apt repository
              cp -f kubernetes_ubuntu.list /etc/apt/sources.list.d/kubernetes.list
              #update apt, install kubelet, kubeadm, kubectl
              apt-get update
              apt-get install -y kubelet kubeadm kubectl

              #config kubelete use contained runtime
              cp -f 0-containerd_ubuntu.conf /etc/systemd/system/kubelet.service.d/0-containerd.conf
             ;;
     esac

#config contained config
cp -f config_k8s.toml /etc/containerd/config.toml

#copy cni net config
cp -rf cni /etc/

sudo systemctl daemon-reload
sudo systemctl restart containerd.service

#configuration system
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab
swapoff -a

sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl --system

